//
//  LS_MainViewController.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class LS_MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewControllers()
        
        //添加自定义tabbar
        let mainTabBar = LS_MainTabBar()
//        print(tabBar.classForCoder)
         //赋值 只读属性不能够直接赋值 但是 可以通过 KVC 间接赋值
//        self.tabBar = mainTabBar
        setValue(mainTabBar, forKey: "TabBar")
//        print(tabBar.classForCoder)
        
         //给加号按钮添加点击事件
        mainTabBar.plusBtn.addTarget(self, action: #selector(LS_MainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
    }
    
    //MARK:加号按钮的点击事件
  @objc private func plusBtnDidClick(){
//        print("哦哦哦哦哦")
    //添加撰写的选择视图
    let composeView = LS_ComposeView()
    composeView.show(targetVC: self)
    
    }

    private func addChildViewControllers(){
        addChildViewController(LS_HomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(LS_DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 1)
        addChildViewController(LS_MessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 2)
        addChildViewController(LS_ProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
    }
    
    func addChildViewController(vc: UIViewController,title: String,imageName: String, index: Int) {
        
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
        //如果标签和导航条的title是一样的话
        let tabBarItem = LS_TabBarItem()
        vc.tabBarItem = tabBarItem
        
        vc.title = title
        
        //给tabBar上的item绑定 tag(给控制器绑定绑定 tag 值)
        vc.tabBarItem.tag = index
        
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中时的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //统一设置所有的title的颜色
        //        tabBar.tintColor = UIColor.orangeColor()
        
        //调整tabBarItme title 的位置
       vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3)
        
        //设置title的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        //内嵌到导航控制器
        let nav = LS_BaseNavViewController(rootViewController: vc)
        
        //添加子控制器
        addChildViewController(nav)
    }
  
    //选中tabBar上面的item就会调用下面的方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //遍历tabBar的子视图
        var index = 0
        
        for v in tabBar.subviews {
            
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //确定点击的是哪一个item
                if index == item.tag {
                    
                    
                    //遍历子视图 获取背景视图
                    for subView in v.subviews {
//                        print(subView)
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            //开启动画 回复 transform 
//                            UIView.animateWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, delay: <#T##NSTimeInterval#>, options: <#T##UIViewAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)
                            
                            //使用带 弹簧效果的动画
                            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 5, options: [], animations: { 
                                subView.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                                    
                            })
                        }
                    }
                }
                index += 1
            }
            
           
        }
        
        
    }
}
